package io.zhifei.modules.sys.pr.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.Query;
import io.zhifei.common.utils.R;
import io.zhifei.modules.sys.pr.entity.Asset;
import io.zhifei.modules.sys.pr.model.params.AssetParam;
import io.zhifei.modules.sys.pr.model.vo.AssetVO;
import io.zhifei.modules.sys.pr.service.AssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 资产管理控制器
 *
 * @author qdp
 * @Date 2021-08-09 14:40:34
 */
@Controller
@RequestMapping("/asset")
@Api(value = "资产控制器",description = ("资产控制器"))
public class AssetController {

    @Autowired
    private AssetService assetService;

    /**
     * 查看资产列表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @ResponseBody
    @GetMapping("/taskList")
    @ApiOperation("查看资产列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "当前页大小", paramType = "query", required = true),
    })
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, message = "请求成功", response = Asset.class)
    })
    public R list(@RequestParam Map<String, Object> pageParam) {
        return R.ok().put("page", new PageUtils(this.assetService.page(new Query<Asset>().getPage(pageParam), Wrappers.<Asset>query().lambda().isNull(Asset::getName))));
    }


    /**
     * 新增或者更新资产接口
     *
     * @author qdp
     * @date 2021-08-09
     */
    @PostMapping("/submit")
    @ApiOperation(value = "新增或者更新资产接口", notes = "新增接口")
    @ResponseBody
    public R submit(@RequestBody AssetParam assetParam) {
        if(this.assetService.addAsset(assetParam))return R.ok();
        else return R.error();
    }

    /**
     * 查看资产列表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @GetMapping("/getAssetPageList")
    @ApiOperation(value = "查询分页列表", notes = "查询分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "当前页大小", paramType = "query", required = true),
    })
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, message = "请求成功", response = Asset.class)
    })
    @ResponseBody
    public R getAssetPageList(@RequestParam Map<String, Object> pageParam) {
        QueryWrapper<Asset> qw = new QueryWrapper<>();
        IPage<Asset> page = new Query<Asset>().getPage(pageParam);
        pageParam.remove("page");
        pageParam.remove("limit");
        qw.allEq(pageParam);
        return R.ok().put("page", new PageUtils(this.assetService.page(page, qw)));
    }

    /**
     * 获取资产树形结构
     */
    @GetMapping("/tree")
    @ApiOperation(value = "树形结构", notes = "树形结构")
    public R tree() {
        List<AssetVO> tree = assetService.tree();
        JSONArray jsonArray = JSONUtil.parseArray(tree);
        return R.ok().put("data", jsonArray);
    }

}
